#include "skypeplugin.h"

SkypePlugin::SkypePlugin()
{
    constructBlinkSequence();

    m_skypeController = new QAxObject("Skype4COM.Skype");
    m_skypeController->dynamicCall("Attach()", 6, false);
    connect(m_skypeController, SIGNAL(signal(QString,int,void*)), this, SLOT(Sink(QString,int,void*)));

    m_missedMessageTimer = new QTimer();
    connect(m_missedMessageTimer, SIGNAL(timeout()), this, SLOT(checkMissedMessages()));
}

SkypePlugin::~SkypePlugin()
{
    delete m_missedMessageTimer;
    delete m_skypeController;
}

QString SkypePlugin::name()
{
    return "Skype Plugin";
}

QList<PluginInterface::Setting> SkypePlugin::settings()
{
    return m_settings;
}

void SkypePlugin::updateSetting(QString name, QVariant value)
{

}

QString SkypePlugin::version()
{
    return VERSION;
}

void SkypePlugin::setEnabled(bool enabled)
{
    if( enabled )
        m_missedMessageTimer->start(60*1000);
    else
        m_missedMessageTimer->stop();
    m_enabled = enabled;
}

void SkypePlugin::constructBlinkSequence()
{
    Blink shortBlink(NUMLEDS);
    shortBlink.setDuration(333);
    for(int i = 0; i < NUMLEDS; ++i)
        shortBlink.setLedColor(i, qRgb(255,255,0));

    Blink shortPause(NUMLEDS);
    shortPause.setDuration(333);
    for(int i = 0; i < NUMLEDS; ++i)
        shortPause.setLedColor(i, 0);

    Blink callFade(NUMLEDS);
    callFade.setFadeInDuration(1000);
    callFade.setDuration(500);
    callFade.setFadeOutDuration(1000);
    for(int i = 0; i < NUMLEDS; ++i)
        callFade.setLedColor(i, qRgb(255,255,0));

    m_missedMessageBlinkSequence.add(shortBlink);
    m_missedMessageBlinkSequence.add(shortPause);
    m_missedMessageBlinkSequence.add(shortBlink);

    m_incommingCallBlinkSequence.add(callFade);
    m_incommingCallBlinkSequence.add(callFade);
    m_incommingCallBlinkSequence.add(callFade);
}

void SkypePlugin::Sink(const QString& name, int argc, void * argv)
{
    if( !m_enabled )
        return;

    if( name.startsWith("MessageStatus") )
    {
        //get number of missed messages
        IDispatch* dis = m_skypeController->property("MissedMessages").value<IDispatch*>();
        QAxObject messageCollection((IChatMessageCollection*)dis);
        int missedMessageCount = messageCollection.property("Count").toInt();
        qDebug() << "number of missed messages: " << missedMessageCount;
        if( missedMessageCount > 0 )
        {
            emit blinkReady(m_missedMessageBlinkSequence);
            m_missedMessageTimer->start(60*1000); //restart the timer
        }
    }
    else if( name.startsWith("CallStatus") )
    {
        VARIANTARG* params = (VARIANTARG*)argv;
        IDispatch* dis = params[argc-1].pdispVal;
        QAxObject call((ICall*)dis);
        int status = call.property("Status").toInt();
        int type = call.property("Type").toInt();
        if( type == 0 || type == 2 ) //cltIncomingPSTN or cltIncomingP2P
            if( status == 4) //clsRinging
                emit blinkReady(m_incommingCallBlinkSequence);
    }
}

void SkypePlugin::checkMissedMessages()
{
    IDispatch* dis = m_skypeController->property("MissedMessages").value<IDispatch*>();
    QAxObject messageCollection((IChatMessageCollection*)dis);
    int missedMessageCount = messageCollection.property("Count").toInt();
    if( missedMessageCount > 0 )
        emit blinkReady(m_missedMessageBlinkSequence);
}

Q_EXPORT_PLUGIN2(skypeplugin, SkypePlugin)
